//
//  FCPListTemplate.swift
//  flutter_carplay
//
//  Created by Oğuzhan Atalay on 21.08.2021.
//

import CarPlay

@available(iOS 14.0, *)
class FCPListTemplate: BaseTabBarTemplate {
    private(set) var _super: CPListTemplate?
    var elementId: String!
    var title: String!
    var systemIcon: String?
    var showsTabBadge: Bool! = false
    private var sections: [CPListSection] = []
    private var objcSections: [FCPListSection] = []
    private var emptyViewTitleVariants: [String] = []
    private var emptyViewSubtitleVariants: [String] = []
    private var templateType: FCPListTemplateTypes
    private var objcBackButton: FCPBarButton?
    private var backButton: CPBarButton?
    private var nextButtonDatas: [CPBarButton]?

    required init(obj: [String : Any]) {
        fatalError("init(obj:) has not been implemented")
    }
    
    required init(obj: [String : Any], templateType: FCPListTemplateTypes) {
        self.templateType = templateType
        self.elementId = obj["_elementId"] as? String
        self.title = obj["title"] as? String
        self.systemIcon = obj["systemIcon"] as? String
        self.emptyViewTitleVariants = obj["emptyViewTitleVariants"] as? [String] ?? []
        self.emptyViewSubtitleVariants = obj["emptyViewSubtitleVariants"] as? [String] ?? []
        self.showsTabBadge = obj["showsTabBadge"] as? Bool  ?? false
        self.objcSections = (obj["sections"] as! Array<[String : Any]>).map {
          FCPListSection(obj: $0)
        }
        self.sections = self.objcSections.map {
          $0.get
        }
        let backButtonData = obj["backButton"] as? [String : Any]
        if backButtonData != nil {
          self.objcBackButton = FCPBarButton(obj: backButtonData!)
          self.backButton = self.objcBackButton?.get
        }
          
        let nextBtnStrs = obj["nextButtons"] as? Array<[String : Any]> ?? nil
        if nextBtnStrs != nil {
            nextButtonDatas =  nextBtnStrs.map {
                $0.map(){
                    FCPBarButton(obj: $0).get
                }
            }
        }
        
    }

    var get: CPTemplate {
        if(_super == nil){
            self._super = CPListTemplate.init(title: title, sections: sections)
        }
        self._super!.emptyViewTitleVariants = emptyViewTitleVariants
        self._super!.emptyViewSubtitleVariants = emptyViewSubtitleVariants
        self._super!.showsTabBadge = showsTabBadge
        if systemIcon != nil {
            self._super!.tabImage = UIImage(systemName: systemIcon!)
        }
        if (templateType == FCPListTemplateTypes.DEFAULT) {
            self._super!.backButton = self.backButton
            if nextButtonDatas != nil {
                self._super!.trailingNavigationBarButtons = nextButtonDatas!
            }
        }
        return self._super!
    }
    
    
  
    public func getSections() -> [FCPListSection] {
        return objcSections
    }
    
    public func update(obj: [String : Any]) {
        self.elementId = obj["_elementId"] as? String
        self.title = obj["title"] as? String
        self.systemIcon = obj["systemIcon"] as? String
        self.emptyViewTitleVariants = obj["emptyViewTitleVariants"] as? [String] ?? []
        self.emptyViewSubtitleVariants = obj["emptyViewSubtitleVariants"] as? [String] ?? []
        self.showsTabBadge = obj["showsTabBadge"] as? Bool  ?? false
        self.objcSections = (obj["sections"] as! Array<[String : Any]>).map {
          FCPListSection(obj: $0)
        }
        self.sections = self.objcSections.map {
          $0.get
        }
        let backButtonData = obj["backButton"] as? [String : Any]
        if backButtonData != nil {
          self.objcBackButton = FCPBarButton(obj: backButtonData!)
          self.backButton = self.objcBackButton?.get
        }
          
        let nextBtnStrs = obj["nextButtons"] as? Array<[String : Any]> ?? nil
        if nextBtnStrs != nil {
            nextButtonDatas =  nextBtnStrs.map {
                $0.map(){
                    FCPBarButton(obj: $0).get
                }
            }
        }
        _ = get
        self._super!.updateSections(self.sections)
    }
    
    
}

@available(iOS 14.0, *)
extension FCPListTemplate: FCPRootTemplate { }
